import axios from 'axios' //引入axios
import OSS from 'ali-oss'
import { Notify } from 'vant';

// 点击上传到oss
export async function ossUploadFile(file) {
    Notify({ type: 'success', message: '正在上传中' });
    let client = new OSS({
        secure: true,
        region: "oss-cn-hangzhou",
        endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
        accessKeyId: "LTAI4FhzzrZfdGcrcQABB2mx",
        accessKeySecret: "r4lMyJpqJd6Pg9Jvu8uMmIeY5e1xM9",
        bucket: "daishu-oss",
    })
    let uuid = getUuid()
    console.log(file.type.split('/'))
    let filePath = '/hr/' + uuid + '.' + file.type.split('/')[1]
    let options = {
        'partSize': 10 * 1024 * 1024,
        'mime': file.type
    }
    await client.multipartUpload(filePath, file, options)
    let fileUrl = 'https://dsimg.chnnice.com/hr/' + uuid + '.' + file.type.split('/')[1];
    Notify.clear();
    return fileUrl
}

function getUuid() {
    var s = []
    var hexDigits = '0123456789abcdef'
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
    }
    s[14] = '4'
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1)
    s[8] = s[13] = s[18] = s[23]
    return s.join('')
}